package com.jspgou.cms.manager.impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.entity.Website;
import com.jspgou.cms.dao.OrderDao;
import com.jspgou.cms.dao.ShopYushouDao;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.OrderItem;
import com.jspgou.cms.entity.Product;
import com.jspgou.cms.entity.ProductFashion;
import com.jspgou.cms.entity.ShopScore;
import com.jspgou.cms.entity.ShopYushou;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.entity.ShopScore.ScoreTypes;
import com.jspgou.cms.manager.ProductFashionMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.ShopScoreMng;
import com.jspgou.cms.manager.ShopYushouMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.cms.web.SiteUtils;

@Service
@Transactional
public class ShopYushouMngImpl implements ShopYushouMng {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public ShopYushou findById(Long id) {
		ShopYushou entity = dao.findById(id);
		return entity;
	}

	public ShopYushou findByCode(String code){
		ShopYushou entity = dao.findByCode(code);
		return entity;
	}
	
	public ShopYushou save(ShopYushou bean) {
		dao.save(bean);
		return bean;
	}

	public ShopYushou update(ShopYushou bean) {
		Updater<ShopYushou> updater = new Updater<ShopYushou>(bean);
		ShopYushou entity = dao.updateByUpdater(updater);
		return entity;
	}

	public ShopYushou deleteById(Long id) {
		ShopYushou bean = dao.deleteById(id);
		return bean;
	}
	
	public ShopYushou[] deleteByIds(Long[] ids) {
		ShopYushou[] beans = new ShopYushou[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private ShopYushouDao dao;

	@Autowired
	public void setDao(ShopYushouDao dao) {
		this.dao = dao;
	}

	public List<ShopYushou> getList(ShopMember member, short status) {
		
		return dao.getList(member, status);
	}

	public Pagination getPage(int pageNo, int pageSize, Long member,Long storeId,Date createTime,
			Short status) {
		
		return dao.getPage(pageNo, pageSize,member,storeId,createTime,status);
	}
	
	public void createYushouOrder(ShopYushou yushouorder,HttpServletRequest request){
		Order order=new Order();
		if(yushouorder.getProductid().getStore()!=null){
			order.setStore(yushouorder.getProductid().getStore());
		}else{
			order.setStore(null);
		}
		order.setIp(request.getRemoteAddr());
		order.setPayment(yushouorder.getPay());
		Website web=SiteUtils.getWeb(request);
		order.setWebsite(web);
		order.setMember(yushouorder.getUser());
		order.setStatus(2);
		order.setShippingStatus(1);//未发货
		order.setPaymentStatus(2);//已支付
		//是预售订单.true:是；false:否；
		order.setRefunded(true);
		order.setReceiveName(yushouorder.getUser().getUsername());
		order.setReceiveAddress(yushouorder.getAddress().getDetailaddress());
		order.setReceiveMobile(yushouorder.getUser().getTel());
		order.setReceivePhone(yushouorder.getUser().getMobile());
		order.setReceiveZip(yushouorder.getAddress().getPostCode());
		order.setAfter(false);
		order.setDeliver(false);
		order.setWeight((double)yushouorder.getWeight());
		order.setFreight(yushouorder.getFreight());
		order.setScore(yushouorder.getProductid().getScore());
		order.setShipping(yushouorder.getShipid());
		order.setProductPrice((yushouorder.getProductid().getDingjin())*yushouorder.getCount()+(yushouorder.getProductid().getWeikuan())*yushouorder.getCount());
		order.setTotal((yushouorder.getProductid().getDingjin())*yushouorder.getCount()+(yushouorder.getProductid().getWeikuan())*yushouorder.getCount()+yushouorder.getFreight());
		order.setCode(yushouorder.getCode());
		order.setCreateTime(yushouorder.getCreatedate());
		order.setLastModified(yushouorder.getCreatedate());
		orderdao.save(order);
	}
	
	@Autowired
	private OrderDao orderdao;
}